<!-- 用户管理 -->
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 用户列表 -->
      <el-col :lg="24" :xs="24">
        <div class="search-bar">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="queryParams.createTime"
                :editable="false"
                class="!w-[240px]"
                type="daterange"
                range-separator="~"
                start-placeholder="开始时间"
                end-placeholder="截止时间"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleQuery">
                <template #icon><Search /></template>
                搜索
              </el-button>
              <el-button @click="handleResetQuery">
                <template #icon><Refresh /></template>
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="never" class="table-wrapper">
          <template #header>
            <div class="flex-x-between">
              <div>
                <el-button
                  v-hasPerm="['sys:user:add']"
                  type="success"
                  @click="handleOpenDialog()"
                >
                  <template #icon><Plus /></template>
                  充值
                </el-button>
              </div>
              <div>
                <el-button class="ml-3" @click="handleExport">
                  <template #icon><Download /></template>
                  导出
                </el-button>
              </div>
            </div>
          </template>
          <el-table
            v-loading="loading"
            :data="pageData"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column
              key="id"
              label="编号"
              align="center"
              prop="id"
              width="180"
            />
            <el-table-column
              key="amount"
              label="金额"
              align="center"
              prop="amount"
              width="120"
            />
            <el-table-column
              key="currency"
              label="币种"
              align="center"
              prop="currency"
              width="120"
            />
            <el-table-column
              key="qtyuantity"
              label="数量"
              align="center"
              prop="quantity"
              width="120"
            />
            <el-table-column
              key="paymentMethod"
              label="支付方式"
              align="center"
              prop="paymentMethod"
              width="120"
            />
            <el-table-column
              key="status"
              label="状态"
              align="center"
              prop="status"
              width="120"
            />
            <el-table-column
              label="创建时间"
              align="center"
              prop="createdAt"
              width="180"
            />
            <el-table-column
              label="支付时间"
              align="center"
              prop="transactionTime"
            />
          </el-table>

          <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryParams.pageNo"
            v-model:limit="queryParams.pageSize"
            @pagination="handleQuery"
          />
        </el-card>
      </el-col>
    </el-row>

    <!-- 用户导入弹窗 -->
    <Recharge
      v-model:visible="dialogVisible"
      @import-success="handleUserImportSuccess"
    />
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "Credit",
  inheritAttrs: false,
});

import CreditAPI, { CreditPageQuery, CreditUsageVO } from "@/api/system/credit";
import TransactionAPI, {
  TransactionPageQuery,
  TransactionVO,
} from "@/api/system/transaction";

import Recharge from "./recharge.vue";

const queryFormRef = ref(ElForm);

const loading = ref(false);
const removeIds = ref([]);
const total = ref(0);
const pageData = ref<TransactionVO[]>();

/** 用户查询参数  */
const queryParams = reactive<TransactionPageQuery>({
  pageNo: 1,
  pageSize: 10,
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  TransactionAPI.list(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.totalCount;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  queryParams.createTime = undefined;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

const dialogVisible = ref(false);

/**
 * 打开弹窗
 *
 * @param id 用户ID
 */
async function handleOpenDialog(id?: number) {
  dialogVisible.value = true;
  // // 加载角色下拉数据源
  // roleOptions.value = await RoleAPI.getOptions();
  // // 加载部门下拉数据源
  // deptOptions.value = await DeptAPI.getOptions();

  // if (id) {
  //   dialog.title = "修改用户";
  //   UserAPI.getFormData(id).then((data) => {
  //     Object.assign(formData, { ...data });
  //   });
  // } else {
  //   dialog.title = "新增用户";
  // }
}

/** 导出用户 */
function handleExport() {
  // UserAPI.export(queryParams).then((response: any) => {
  //   const fileData = response.data;
  //   const fileName = decodeURI(
  //     response.headers["content-disposition"].split(";")[1].split("=")[1]
  //   );
  //   const fileType =
  //     "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8";
  //   const blob = new Blob([fileData], { type: fileType });
  //   const downloadUrl = window.URL.createObjectURL(blob);
  //   const downloadLink = document.createElement("a");
  //   downloadLink.href = downloadUrl;
  //   downloadLink.download = fileName;
  //   document.body.appendChild(downloadLink);
  //   downloadLink.click();
  //   document.body.removeChild(downloadLink);
  //   window.URL.revokeObjectURL(downloadUrl);
  // });
}

function handleUserImportSuccess() {}
onMounted(async () => {
  handleQuery();
});
</script>
